package com.rw.coco.handler.message.reply.handler.validate;

import com.rw.coco.api.data.PersistenceMapper;
import com.rw.coco.api.data.model.AccountModel;
import com.rw.coco.api.reply.mapper.AdvancedReply;
import com.rw.coco.handler.message.event.CocoMessageEvent;
import com.rw.coco.handler.message.reply.ReplyHandler;
import com.rw.coco.handler.message.reply.handler.command.assistant.CommandMatcher;
import com.rw.coco.handler.message.reply.handler.transaction.TransactionAble;
import com.rw.coco.util.config.CocoCommandConfig;
import com.rw.coco.util.config.CocoConfig;

/**
 * 黑名单指令
 * 需要开启事务
 */
@TransactionAble
public class BlackListHandler implements ReplyHandler {
    private final String blackCommand;

    private final String whiteCommand;

    private final String blackGroupCommand;

    private final String whiteGroupCommand;

    private final CommandMatcher matcher;

    public BlackListHandler(CocoConfig cocoConfig) {
        CocoCommandConfig cocoCommandConfig = cocoConfig.getCocoCommandConfig();

        blackCommand = cocoCommandConfig.commandProp.get(CocoCommandConfig.BLACK_USER);
        whiteCommand = cocoCommandConfig.commandProp.get(CocoCommandConfig.WHITE_USER);
        blackGroupCommand = cocoCommandConfig.commandProp.get(CocoCommandConfig.BLACK_GROUP);
        whiteGroupCommand = cocoCommandConfig.commandProp.get(CocoCommandConfig.WHITE_GROUP);

        matcher = CommandMatcher.of(blackCommand, whiteCommand, blackGroupCommand, whiteGroupCommand);
    }

    @Override
    public boolean trigger(CocoMessageEvent cocoMessageEvent) {
        if (!cocoMessageEvent.hasPrefix()) {
            return false;
        }

        // 如果不是超级管理员，直接返回
        if (!cocoMessageEvent.getAccountModel().getSuperAdmin()) {
            return false;
        }

        // 返回指令是否匹配
        String message = cocoMessageEvent.getMessage();
        return matcher.isMatch(message);
    }

    @Override
    public void handler(CocoMessageEvent cocoMessageEvent) {
        // 去掉前缀
        String message = cocoMessageEvent.getMessage();

        // 获取ID
        String id = matcher.sub(message).trim();

        // 是否是黑名单
        boolean black = blackCommand.equals(matcher.assertMatch(message)) ||
                blackGroupCommand.equals(matcher.assertMatch(message));

        // 是否是群组
        boolean group = whiteGroupCommand.equals(matcher.assertMatch(message)) ||
                blackGroupCommand.equals(matcher.assertMatch(message));

        // 加入黑名单
        black(cocoMessageEvent, id, black, group);

        // 回复
        reply(cocoMessageEvent, id, black, group);
    }

    /**
     * 放入黑名单
     */
    private void black(CocoMessageEvent cocoMessageEvent, String id, boolean black, boolean group) {
        PersistenceMapper mapper = cocoMessageEvent.getPersistenceMapper();
        // 分类讨论群组和用户
        if (group) {
            // 如果群组不存在，则直接加入空壳群组
            mapper.getGroupMapper().getGroupOrPut(id);
            // 操作黑名单
            mapper.getGroupMapper().blackOrWhiteGroup(id, black);
        } else {
            // 如果用户不存在，先加入空壳用户
            mapper.getAccountMapper().getAccountOrPut(id, AccountModel.getDefault(id, ""));
            // 操作黑名单
            mapper.getAccountMapper().blackOrWhiteAccount(id, black);
        }
    }

    /**
     * 回复
     */
    private void reply(CocoMessageEvent cocoMessageEvent, String id, boolean black, boolean group) {
        AdvancedReply advancedReply = cocoMessageEvent.getReplyApi().getAdvancedReply();

        // 根据群组/用户、黑白名单进行分类讨论
        if (group) {
            if (black) {
                cocoMessageEvent.sendBack(advancedReply.blackGroup(cocoMessageEvent, id));
            } else {
                cocoMessageEvent.sendBack(advancedReply.whiteGroup(cocoMessageEvent, id));
            }
        } else {
            if (black) {
                cocoMessageEvent.sendBack(advancedReply.blackUser(cocoMessageEvent, id));
            } else {
                cocoMessageEvent.sendBack(advancedReply.whiteUser(cocoMessageEvent, id));
            }
        }
    }
}
